package cn.biao.test.server;

import cn.biao.test.tservice.HelloWorldService;
import cn.biao.test.tservice.impl.HelloWorldServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;

/**
 * @author lino
 */
@Slf4j
public class ThriftServer {
    private static final int PORT = 2345;

    private void startServer(){
        HelloWorldService.Processor processor = new HelloWorldService.Processor<HelloWorldService.Iface>(new HelloWorldServiceImpl());
        try {
            TServerTransport transport = new TServerSocket(PORT);
            TBinaryProtocol.Factory factory = new TBinaryProtocol.Factory();
            TServer.Args tArgs = new TServer.Args(transport);
            tArgs.processor(processor);
            tArgs.protocolFactory(factory);
            TServer tServer = new TSimpleServer(tArgs);
            System.out.println("Thrift服务启动");
            tServer.serve();
        } catch (Exception e){
            e.printStackTrace();
            System.out.println("Thrift Server 启动失败");
        }
    }

    public static void main(String[] args) {
        ThriftServer thriftServer = new ThriftServer();
        thriftServer.startServer();
    }
}
